package com.taosdata.jdbc.springbootdemo.serviceImpl.tdengine;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taosdata.jdbc.springbootdemo.config.beanContext.BeanContext;
import com.taosdata.jdbc.springbootdemo.domain.stables.SuperTable;
import com.taosdata.jdbc.springbootdemo.mapper.tdengine.stableMapper.StableMapper;
import com.taosdata.jdbc.springbootdemo.service.tdengine.StableService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zhmsky
 * @date 2022/9/25 14:26
 */
@Service
public class StableServiceImpl extends ServiceImpl<StableMapper, SuperTable> implements StableService {

    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void insertCommonData(List<SuperTable> list) {
        StableMapper stableMapper = BeanContext.getApplicationContext().getBean(StableMapper.class);
        SuperTable superTable = list.get(0);
        String constructionId = superTable.getConstructionId();
        String monitorTypeId = superTable.getMonitorTypeId();
        String stableName = "td_" + constructionId + "_" + monitorTypeId;
        String subTableName = superTable.getSensorId();
        //1. 创建超级表
        lock.lock();
        try {
            stableMapper.createStable(stableName);
        } finally {
            lock.unlock();
        }
        // 2.入库
        stableMapper.insertBatchCommonData(superTable, list, stableName, subTableName);
    }

}